package edu.xhu.communityantiepidemicsystemback.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.xhu.communityantiepidemicsystemback.common.result.PageResult;
import edu.xhu.communityantiepidemicsystemback.common.result.Result;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Community;
import edu.xhu.communityantiepidemicsystemback.entity.Material;
import edu.xhu.communityantiepidemicsystemback.service.IMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/material")
@Slf4j
public class MaterialController {


    @Autowired
    private IMaterialService materialService;


    @PostMapping("/all")
    public PageResult<?> allMaterialInfo(@RequestBody Query query){
        IPage<Material> materialIPage= materialService.getAllMaterialInfo(query);
        if(Objects.isNull(materialIPage)){
            return new PageResult<>();
        }
        return new PageResult<>(materialIPage.getTotal(),materialIPage.getRecords());
    }

    //对仓库物资进行查询
    @PostMapping("/fuzzysearch")
    public PageResult<?> fuzzyMaterialInfo(@RequestBody Fuzzy fuzzy){
        IPage<Material> materialPage =materialService.fuzzyMaterialInfo(fuzzy);
        if (Objects.isNull(materialPage)){
            return new PageResult<>();
        }
        return new PageResult<Material>(materialPage.getTotal(),materialPage.getRecords());
    }
    //对仓库物资进行添加
    @PostMapping("/add")
    public Result<?> addMaterial(@RequestBody CreatMaterial creatMaterial){
        boolean result=materialService.addMaterial(creatMaterial);
        if(result){
            return new Result<>().success();
        }else{
            return new Result<>().error("添加物资信息失败！");
        }
    }
    //对仓库物资进行删除
    @GetMapping("/del/{id}")
    public Result<?> delMaterial(@PathVariable Integer id){
        boolean result =materialService.delMaterial(id);
        if (result){
            return new Result<>().success();
        }else{
            return new Result<>().error("删除物资信息失败,服务器内部错误");
        }
    }
    //对仓库物资信息进行修改
    @PostMapping("/update")
    public Result<?> update(@RequestBody UpdateMaterial creatMaterial){
        boolean result =materialService.updateMaterial(creatMaterial);
        if (result){
            return new Result<>().success();
        }else{
            return new Result<>().error("修改物资信息失败，服务器内部错误");
        }
    }

}
